package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.config.WebConfig;
import com.example.entity.UserDO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.cxf.endpoint.Client;

import org.springframework.web.bind.annotation.*;

/**
 * @Create by Intellij IDEA
 * @Author: songXuSheng
 * @Date: 2021/9/10 09:25
 */

@RestController
@RequestMapping("/serviced")
@CrossOrigin
public class UserController {
    /**
     * 查询单个用户
     * @param id 用户id
     * @return User 单个用户信息
     */
    @GetMapping("/{id}")
    public UserDO findById(@PathVariable("id") String id){
        Client client = WebConfig.getClient();
        Object[] invoke = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            invoke = client.invoke("findById", id);
            UserDO userDO=null;
            for (Object o : invoke) {
                 userDO = JSON.parseObject(o.toString(), UserDO.class);
            }
            return userDO;
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 添加用户
     * @param user 用户信息
     * @return true/false
     */
    @PostMapping("/add")
    public boolean add(@RequestBody UserDO user){
        String UserStr = JSON.toJSONString(user);
        Client client = WebConfig.getClient();
        Object[] invoke = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            invoke = client.invoke("addUserDO", UserStr);
            String str="";
            for (Object o : invoke) {
                str=o.toString();
            }
            if (str == "1" || "1".equals(str)){
                return true;
            }else {
                return false;
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 查询所有用户
     * @param current 当前页
     * @param limit 每页数量
     * @return Page对象
     */

    @GetMapping("/findAll/{current}/{limit}")
    public Page<UserDO> listUser(@PathVariable Long current, @PathVariable Long limit){
        Client client = WebConfig.getClient();
        Object[] invoke = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            invoke = client.invoke("listUser", current,limit);
            Page page=null;
            for (Object o : invoke) {
                page = JSON.parseObject(o.toString(), Page.class);
            }
            return page;
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 删除用户
     * @param id 用户ID
     * @return true/false
     */
    @DeleteMapping("/{id}")
    public boolean delete(@ApiParam(name = "id", value = "用户id", required = true) @PathVariable String id){
        Client client = WebConfig.getClient();
        Object[] invoke = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            invoke = client.invoke("delete", id);
            String str="";
            for (Object o : invoke) {
                str=o.toString();
            }
            if (str == "1" || "1".equals(str)){
                return true;
            }else {
                return false;
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 更新用户
     * @param user 用户信息
     * @return true/false
     */
    @ApiOperation(value = "更新用户")
    @PostMapping("/update")
    public boolean update(@RequestBody UserDO user){
        String UserStr = JSON.toJSONString(user);
        Client client = WebConfig.getClient();
        Object[] invoke = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            invoke = client.invoke("update", UserStr);
            String str="";
            for (Object o : invoke) {
                str=o.toString();
            }
            if (str == "1" || "1".equals(str)){
                return true;
            }else {
                return false;
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
